package com.kob.kobbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kob.kobbackend.mapper.UserMapper;
import com.kob.kobbackend.pojo.User;
import com.kob.kobbackend.service.user.account.UserAccountService;
import com.kob.kobbackend.utils.JwtUtil;
import com.kob.kobbackend.utils.UserDetailsImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserAccountServiceImpl implements UserAccountService {
    @Autowired
    private AuthenticationManager authenticationManager;
    
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    //实现登录接口
    @Override
    public Map<String, String> getToken(String username, String password) {
        UsernamePasswordAuthenticationToken authenticationToken =
                new UsernamePasswordAuthenticationToken(username, password);
        Authentication authentication = authenticationManager.authenticate(authenticationToken);

        UserDetailsImpl loginUser = (UserDetailsImpl) authentication.getPrincipal();
        User user = loginUser.getUser();
        String jwt = JwtUtil.createJWT(user.getId().toString());
        System.out.println(jwt);
        Map<String,String> map = new HashMap<>();
        map.put("result","success");
        map.put("jwt_token",jwt);
        return map;
    }

    //实现获取信息接口
    @Override
    public Map<String, String> getinfo() {
        UsernamePasswordAuthenticationToken authenticationToken =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
        User user = loginUser.getUser();

        Map<String,String> map = new HashMap<>();
        map.put("result","success");
        map.put("id",user.getId().toString());
        map.put("username",user.getUsername());
        map.put("photo",user.getPhoto());
        return map;
    }

    //实现注册接口
    @Override
    public Map<String, String> register(String username, String password, String confirmPassword) {
        Map<String, String> map = new HashMap<>();

        if(username==null){
            map.put("result","用户名不能为空");
            return map;
        }

        if(password==null || confirmPassword==null){
            map.put("result","密码不能为空");
            return map;
        }

        username = username.trim();
        if(username.isEmpty()){
            map.put("result","用户名不能为空");
            return map;
        }

        if(password.isEmpty() || confirmPassword.isEmpty()){
            map.put("result","密码不能为空");
            return map;
        }

        if(username.length() > 100){
            map.put("result","用户名长度不能大于100");
            return map;
        }

        if(password.length() > 100 || confirmPassword.length() > 100){
            map.put("result","密码长度不能大于100");
            return map;
        }

        if(!password.equals(confirmPassword)){
            map.put("result","两次输入的密码不一致");
            return map;
        }

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        List<User> users = userMapper.selectList(queryWrapper);

        if(!users.isEmpty()){
            map.put("result","用户名已存在");
            return map;
        }

        String encodePassword = passwordEncoder.encode(password);
        String photo = "https://www.helloimg.com/i/2024/12/22/6767fd5a3cf23.jpg";
        User user = new User(null, username, encodePassword, photo, 1500);
        userMapper.insert(user);
        map.put("result","success");
        return map;
    }
}
